from tkinter import *
from random import randint
import time 


class MyBall:
    width = 800
    height = 600
    vx = 2
    D = 10
    DURATION = 0.001   # sleep 时间 = 绘制的间隔
    def __init__(self,color):
        self.tk = Tk()
        self.canvas =Canvas(self.tk, width=MyBall.width, height=MyBall.height,bd=2)
        self.make_walls()
        self.ball = self.canvas.create_oval(10,50,30,70,fill=color,outline="lightgray")
        self.canvas.pack()

    def make_walls(self):
        self.canvas.create_rectangle(0, 0, MyBall.width, MyBall.height, fill="white")
        
    def draw(self):
        x = 10
        for s in range(6000):
           x = x + MyBall.vx

           if x + MyBall.D > MyBall.width :
               MyBall.vx = -MyBall.vx
               
           if x + MyBall.D < 0 :
               MyBall.vx = -(MyBall.vx)
               
           self.canvas.move(self.ball, MyBall.vx, 0)
           self.tk.update()
           time.sleep(MyBall.DURATION)
           
        self.tk.mainloop()
        
MyBall= MyBall("red")
MyBall.draw()